package com.yanqu.road.entity.tradewar;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class UserCrossTradeWarTeam extends DataObject {
    private long userId;                             //玩家编号
    private String patronsTeamListString;            //队伍数据
    private List<List<Integer>> patronsTeamList = new ArrayList<>();     //队伍
    private String buyBuffTimesListString;          //鼓舞次数数据
    private List<Integer> buyBuffTimesList = new ArrayList<>();         //鼓舞次数

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        if(this.userId != userId) {
            this.userId = userId;
            setUpdateOption();
        }
    }

    public String getPatronsTeamListString() {
        return patronsTeamListString;
    }

    public void setPatronsTeamListString(String patronsTeamListString) {
        if(!Objects.equals(this.patronsTeamListString, patronsTeamListString)) {
            this.patronsTeamListString = patronsTeamListString;
            setUpdateOption();
        }
    }

    public void addPatronsTeam(List<Integer> patronsIdList){
        patronsTeamList.add(patronsIdList);
        String listStr = parsePatronsTeamListString(patronsTeamList);
        setPatronsTeamListString(listStr);
    }

    public void changePatronsTeam(int index, List<Integer> patronsIdList){
        if(index < 0 || index >= patronsTeamList.size()){
            return;
        }
        int i = 0;
        Iterator<List<Integer>> iterator = patronsTeamList.iterator();
        while (iterator.hasNext()){
            List<Integer> dataList = iterator.next();
            if(i == index){
                dataList.clear();
                dataList.addAll(patronsIdList);
                break;
            }
            i++;
        }
        String listStr = parsePatronsTeamListString(patronsTeamList);
        setPatronsTeamListString(listStr);
    }

    public void removePatronsTeam(int index){
        if(index < 0 || index >= patronsTeamList.size()){
            return;
        }
        int i = 0;
        Iterator<List<Integer>> iterator = patronsTeamList.iterator();
        while (iterator.hasNext()){
            iterator.next();
            if(i == index){
                iterator.remove();
                break;
            }
            i++;
        }
        String listStr = parsePatronsTeamListString(patronsTeamList);
        setPatronsTeamListString(listStr);
    }

    public List<List<Integer>> getPatronsTeamList() {
        return patronsTeamList;
    }

    public List<Integer> getPatronsIdList(int teamNum){
        int idx = teamNum;
        if (idx < 0 || idx >= patronsTeamList.size()){
            return new ArrayList<>();
        }
        return patronsTeamList.get(idx);
    }

    public void setPatronsTeamList(List<List<Integer>> patronsTeamList) {
        this.patronsTeamList = patronsTeamList;
    }

    public void setPatronsTeamList(String patronsTeamListStr) {
        if(StringUtils.isNullOrEmpty(patronsTeamListStr)){
            this.patronsTeamList = new ArrayList<>();
            return;
        }
        String[] teamArr = patronsTeamListStr.split("\\|");
        List<List<Integer>> teamList = new ArrayList<>();
        for(int i = 0; i < teamArr.length; i++){
            if(!StringUtils.isNullOrEmpty(teamArr[i])) {
                List<Integer> dataList = StringUtils.stringToIntegerList(teamArr[i], ",");
                teamList.add(dataList);
            }
        }
        this.patronsTeamList = teamList;
    }

    private String parsePatronsTeamListString(List<List<Integer>> dataList){
        String listStr = "";
        for(List<Integer> tempList : dataList){
            listStr = listStr + StringUtils.listToString(tempList, ",");
            listStr = listStr + "|";
        }
        if(listStr.length() > 0){
            return listStr.substring(0, listStr.length() - 1);
        }
        return listStr;
    }

    public String getBuyBuffTimesListString() {
        return buyBuffTimesListString;
    }

    public void setBuyBuffTimesListString(String buyBuffTimesListString) {
        if(!Objects.equals(this.buyBuffTimesListString, buyBuffTimesListString)) {
            this.buyBuffTimesListString = buyBuffTimesListString;
            setUpdateOption();
        }
    }

    public List<Integer> getBuyBuffTimesList() {
        return buyBuffTimesList;
    }

    public void setBuyBuffTimesList(List<Integer> buyBuffTimesList) {
        this.buyBuffTimesList = buyBuffTimesList;
    }

    public void setBuyBuffTimesList(String buyBuffTimesListStr) {
        this.buyBuffTimesList = StringUtils.stringToIntegerList(buyBuffTimesListStr, "\\|");
    }

    public void removeBuyBuffTimes(int index){
        if(index < 0 || index >= buyBuffTimesList.size()){
            return;
        }
        int i = 0;
        Iterator<Integer> iterator = buyBuffTimesList.iterator();
        while (iterator.hasNext()){
            iterator.next();
            if(i == index){
                iterator.remove();
                break;
            }
            i++;
        }
        setBuyBuffTimesListString(StringUtils.listToString(buyBuffTimesList, "|"));
    }

    public int getBuyBuffTimes(int index){
        if(index < 0 || index >= buyBuffTimesList.size()){
            return 0;
        }
        return buyBuffTimesList.get(index);
    }

    public void setBuyBuffTimes(int index, int type, int teamSize){
        if(index < 0 || index >= teamSize){
            return;
        }
        int buffSize = buyBuffTimesList.size();
        if(buffSize < teamSize){
            int offset = teamSize - buffSize;
            for(int i = 0; i < offset; i++){
                buyBuffTimesList.add(0);
            }
        }
        buyBuffTimesList.set(index, type);
        String listStr = StringUtils.listToString(buyBuffTimesList, "|");
        setBuyBuffTimesListString(listStr);
    }
}
